package com.teamall.web.tssy.controller;

import java.math.*;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.teamall.core.common.BaseController;
import com.teamall.framework.util.DateUtils;
import com.teamall.core.common.DwzPage;
import com.teamall.core.common.JModelAndView;
import com.teamall.core.common.Page;
import com.teamall.framework.util.DateUtils;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.web.tssy.db.po.TsSyUserRolePO;
import com.teamall.web.tssy.db.po.TsSyUserRolePOEX;
import com.teamall.web.tssy.service.ITsSyUserRoleService;

/**
 * <Description>tssyuserrole管理 <br>
 * 
 * @author codeCreater <br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.teamall.web.tssy.controller <br>
 */

@Controller
@RequestMapping("/tssy/tsSyUserRole")
public class TsSyUserRoleController extends BaseController {

    private static final LogManager logger = LogManager.getLogger(TsSyUserRoleController.class);

    @Autowired
    private ITsSyUserRoleService tsSyUserRoleService;
    
    @RequestMapping("index")
    public String index(Model model) {
        // ///////
        // TODO 根据业务场景，进行条件分支跳转、设置页面默认值等

        // ///////

        return "/page/tssy";
    }

    @RequestMapping("queryRecordByPage")
    public ModelAndView queryRecordByPage(TsSyUserRolePO record,
            Page page,HttpServletRequest request) throws BaseAppException {
    	logger.info("queryRecordByPage by record begin...record=[{0}]", record);
    	ModelAndView mv = new JModelAndView("tssy/tsSyUserRoleList",request);
    	//将dwz分页参数转成Page对象
//    	Page page = dwzToPage(dwzPage);
    	Page<TsSyUserRolePO> resultPage = tsSyUserRoleService.selectByArgAndPage(record, page);
    	mv.addObject("resultPage", resultPage);
    	mv.addObject("record", record);
    	
    	return mv;
    }

    @RequestMapping("initAdd")
    public ModelAndView initAdd(TsSyUserRolePO record,HttpServletRequest request) throws BaseAppException {
        logger.info("initAdd add begin...record=[{0}]", record);
        ModelAndView mv = new JModelAndView("tssy/tsSyUserRoleAdd", request);
        return mv;
    }
    
    @RequestMapping(value="add",method=RequestMethod.POST)
    public ModelAndView add(TsSyUserRolePO record) throws BaseAppException {
        logger.info("add record begin...record=[{0}]", record);
        tsSyUserRoleService.add(record);
        return ajaxDoneSuccess(this.operSuccess);
    }

    @RequestMapping(value="update",method=RequestMethod.PUT)
    public ModelAndView update(TsSyUserRolePO record) throws BaseAppException {
        logger.info("update record begin...record=[{0}]", record);
        tsSyUserRoleService.update(record);
        return ajaxDoneSuccess(this.operSuccess);
    }

    @RequestMapping(value = "delete",method=RequestMethod.DELETE)
    public ModelAndView delete(@RequestParam(value = "id",required = true) String id) throws BaseAppException {
        logger.info("delete record begin...id=[{0}]", id);
        tsSyUserRoleService.updateStatus(id);
        return ajaxDoneSuccess(operSuccess);
    }

    @RequestMapping("qryRecordInfo")
    public ModelAndView qryRecordInfo(@RequestParam(value = "id",
            required = true) String id,HttpServletRequest request) throws BaseAppException {
    	logger.info("qryRecordInfo by id begin...id=[{0}]", id);
    	ModelAndView mv = new JModelAndView("tssy/tsSyUserRoleAdd",request);
    	TsSyUserRolePO record = tsSyUserRoleService.selectByPrimaryKey(id);
    	mv.addObject("record", record); 
    	return mv;  
    }

    @RequestMapping("queryPageForLookup")
    public ModelAndView queryPageForLookup(TsSyUserRolePOEX record,
            Page page,HttpServletRequest request) throws BaseAppException {
    	logger.info("queryRecordByPage by record begin...record=[{0}]", record);
    	ModelAndView mv = new JModelAndView("tssy/tsSyUserRoleListEX",request);
    	//将dwz分页参数转成Page对象
//    	Page page = dwzToPage(dwzPage);
    	List<TsSyUserRolePOEX> result = tsSyUserRoleService.selectListTsSyUserRolePOEX(page.getStart(), page.getLimit(), record);
    	int count = tsSyUserRoleService.selectCountTsSyUserRolePOEX(page.getStart(), page.getLimit(), record);
    	Page<TsSyUserRolePOEX> resultPage = new Page<TsSyUserRolePOEX>();
    	resultPage.setResultList(result);
    	resultPage.setLimit(page.getLimit());
    	resultPage.setStart(page.getStart());
    	resultPage.setTotalRecords(count);
    	mv.addObject("record", record);
    	mv.addObject("resultPage", resultPage);
    	mv.addObject("userId",record.getUserId());
    	return mv;
    }
    
    @RequestMapping(value="saveUserRole",method=RequestMethod.POST)
    @ResponseBody
    public String saveUserRole(@RequestParam(value = "flag",
            required = true) String flag,@RequestParam(value = "userId",
            required = true) String userId,@RequestParam(value = "roleId",
            required = true) String roleId)throws BaseAppException{
    	TsSyUserRolePO userRole = new TsSyUserRolePO();
    	userRole.addNewConditions("roleId", roleId);
    	userRole.addNewConditions("userId", userId);
    	List<TsSyUserRolePO> listRR = tsSyUserRoleService.selectByArg(userRole);
    	if("1".equals(flag)){
    		if(!CollectionUtils.isEmpty(listRR)){
    		}else{
    			TsSyUserRolePO record = new TsSyUserRolePO();
    			record.setUserId(userId);
    			record.setRoleId(roleId);
    			tsSyUserRoleService.add(record);
    		}
    	}else if("0".equals(flag)){
    		if(!CollectionUtils.isEmpty(listRR)){
    			TsSyUserRolePO record = listRR.get(0);
    			tsSyUserRoleService.delete(record);
    		}else{
    		}
    	}
    	return "";
    }
}
